<?php
//  This file is part of EkkinCMS.
//
//  EkkinCMS is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  EkkinCMS is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with EkkinCMS.  If not, see <http://www.gnu.org/licenses/>.

// sql


class SQL {

    public function __construct( $sql_username, $sql_password, $sql_hostname, $sql_database ){
        try {
            //echo "$sql_username, $sql_password";
            $this->db = new PDO( "mysql:host=$sql_hostname;dbname=$sql_database", $sql_username, $sql_password );

            $this->db_name = $sql_database;
            //echo "DB connected<br/>";
            }
        catch(PDOException $e)
            {
            echo $e->getMessage();
            }
    }

    public function create_table( $table_name ){
        $count = $this->exec( "CREATE TABLE IF NOT EXISTS $table_name (id int)" );
        $this->exec( "INSERT INTO $table_name (id) VALUES (0)" );
    }

    public function field_exists( $table_name, $field ){
        $r = $this->query( "SELECT * FROM $table_name" );
        if ($r[$field] == NULL) {
            return false;
        }
        else {
            return true;
        }
    }

    public function add_field( $table_name, $field ){
        //if (! $this->field_exists( $table_name, $field )){
            $this->exec( "ALTER TABLE $table_name ADD $field text");
        //}
        //else {
        //    echo "already added";
        //}
    }

    public function get_value( $table_name, $field, $vfield, $id ){
        echo "depreciated<br/>";
        $r = $this->query( "SELECT * FROM $table_name" );
        foreach($r as $row){
            if ($row[$field] == $id){
                return $row[$vfield];
            }
        }
        //print_r($r);
    }

    public function get_values_c( $table_name, $condition ){
        $r = $this->query( "SELECT * FROM $table_name WHERE $condition" );
        return $r[0];
    }

    public function insert_value( $table_name, $key, $value){
        //$this->exec( "DELETE FROM $table_name WHERE
        $this->exec( "INSERT INTO $table_name (key, value) VALUES ($key, \"$value\")" );
    }

    public function exec($sql){
        try {
            //echo "exec: $sql <br/> ";
            $r = $this->db->exec( $sql );
            return $r;
        }
        catch( PDOException $e ) {
            echo $e->getMessage();
        }
    }
    public function query($sql){
        try {
            //echo "query: $sql</br>";
            $s = $this->db->query( $sql );

            $r = $s->fetchAll(PDO::FETCH_ASSOC);

            return $r;
        }
        catch( PDOException $e ) {
            echo $e->getMessage();
        }
    }
}
?>



